<template>
  <div class="app-container">
    <el-form ref="formView" :model="formView" label-width="80px" :inline="true">
      <el-form-item label="姓名" prop="staffName">
        <el-input
          v-model="detailQuery.staffName"
          placeholder="请输入员工姓名"
          clearable
          size="small"
          @keyup.enter.native="handleViewTableQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="detailQuery.idCard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleViewTableQuery"
        />
      </el-form-item>
      <el-form-item label="岗位" prop="stationName">
        <el-input
          v-model="detailQuery.stationName"
          placeholder="请输入岗位"
          clearable
          size="small"
          @keyup.enter.native="handleViewTableQuery"
        />
      </el-form-item>

      <el-form-item label="组别" prop="stationName">
        <el-input
          v-model="detailQuery.groupName"
          placeholder="请输入组别"
          clearable
          size="small"
          @keyup.enter.native="handleViewTableQuery"
        />
      </el-form-item>


      <el-row type="flex">
        <el-col>
          <el-form-item style="margin: 0 40px">
            信息: {{ title }}
          </el-form-item>
          <el-form-item style="margin: 0 20px">
            当前人数:{{ tableData.length }}
          </el-form-item>
        </el-col>

        <el-col>
          <el-form-item style="float: right">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleViewTableQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetDetailQuery">重置</el-button>
            <el-button
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImportToCover"
              title="注意:需上传原文件进行覆盖"
              v-hasPermi="['job:termSchedule:cover']"
            >覆盖
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table
      :data="tableData"
      border
      v-loading="userloading"
      style="width: 100%"
      height="600px"
    >
      <el-table-column
        prop="staffName"
        label="姓名"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="idCard"
        label="身份证"
        width="200"
      ></el-table-column>

      <el-table-column
        prop="deptName"
        label="班组名称"
        width="100"
      ></el-table-column>

      <el-table-column
        prop="groupName"
        label="组别"
        width="100"
      ></el-table-column>

      <el-table-column
        prop="areaName"
        label="区域名称"
        width="100"
      ></el-table-column>

      <el-table-column
        prop="stationName"
        label="岗位"
        width="100"
      ></el-table-column>

      <el-table-column align="center" v-for="(item,index) in planDays" :key="index + item" :label="item" width="auto" min-width="100px"
                       :prop="item">
        <template slot-scope="scope">
          <div>{{ scope.row.planDays[index].attendanceGroupName }}</div>
        </template>
      </el-table-column>
    </el-table>

    <!--  排班模板上传 -s  -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :on-change="beforeUpload"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件</span>
        </div>

      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="uploadCancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  importSchedule,
  getTermSchedule,
} from "@/api/job/termSchedule/index_new";
import { getToken } from "@/utils/auth";

export default {
  name: "index.vue",
  data(){
    return{
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: '',
      },
      title:this.$route.query.title,
      planDays:[],
      tableData:[],
      userloading:true,
      formView:{},
      detailQuery:{
        scheduleTaskId :this.$route.query.scheduleTaskId,
      },
    }
  },
  created() {
    console.log("this.$route.query",this.$route.query)
    this.handleViewShow();
  },
  methods: {
    //详情接口
    handleViewShow(){
      this.planDays = [];
      getTermSchedule(this.detailQuery).then(response => {
        console.log(response)
        let list = response.data;
        if (list.length>0){
          let days = list[0].planDays;
          for (let day of days) {
            this.planDays.push(day.scheduleDate);
          }
        }
        this.tableData = list;
        this.userloading = false;
      });
    },
    //点击查询事件
    handleViewTableQuery() {
      this.handleViewShow(this.detailQuery);
    },
    /** 重置详情按钮操作 */
    resetDetailQuery() {
      this.resetForm("formView");
      let query = this.detailQuery;
      this.detailQuery={
        scheduleTaskId:query.scheduleTaskId
      }
    },
    /** 覆盖按钮操作 */
    handleImportToCover() {
      this.upload.flag = 2;
      this.upload.url ="/scheduling/plan/coverSchedulingPlanTemplate?id="+this.detailQuery.scheduleTaskId;
      this.upload.title = "覆盖原排班信息";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", {dangerouslyUseHTMLString: true});
    },
    beforeUpload(file, fileList) {
      this.fileList = fileList;
    },
    // 提交上传文件
    submitFileForm() {
      this.upload.isUploading = true;
      importSchedule(this.upload.url, this.fileList).then(res => {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.handleViewShow(this.detailQuery);
      });
    },
    /** 上传取消 */
    uploadCancel() {
      this.upload.open = false;
      this.$refs.upload.clearFiles();
    },

  }
}
</script>

<style scoped>

</style>
